package repository

import (
    "gorm.io/gorm"
    "generator-tool/models"
)

type ShGoodsCategoryRepository struct {
    DB *gorm.DB
}

func NewShGoodsCategoryRepository(db *gorm.DB) *ShGoodsCategoryRepository {
    return &ShGoodsCategoryRepository{DB: db}
}

func (r *ShGoodsCategoryRepository) CreateShGoodsCategory(ShGoodsCategory *models.ShGoodsCategory) error {
    return r.DB.Create(ShGoodsCategory).Error
}

func (r *ShGoodsCategoryRepository) GetAllShGoodsCategorys() ([]models.ShGoodsCategory, error) {
    var ShGoodsCategorys []models.ShGoodsCategory
    err := r.DB.Find(&ShGoodsCategorys).Error
    return ShGoodsCategorys, err
}

func (r *ShGoodsCategoryRepository) GetShGoodsCategoryByID(id uint) (*models.ShGoodsCategory, error) {
    var ShGoodsCategory models.ShGoodsCategory
    err := r.DB.First(&ShGoodsCategory, id).Error
    if err != nil {
        return nil, err
    }
    return &ShGoodsCategory, nil
}

func (r *ShGoodsCategoryRepository) UpdateShGoodsCategory(ShGoodsCategory *models.ShGoodsCategory) error {
    return r.DB.Save(ShGoodsCategory).Error
}

func (r *ShGoodsCategoryRepository) DeleteShGoodsCategory(id uint) error {
    return r.DB.Delete(&models.ShGoodsCategory{}, id).Error
}